import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/net/response/event_manage/event_manage_list_data.dart';
import 'package:ls_work/res/colors.dart';

import '../../net/response/net_file_data.dart';
import '../../viewmodel/event_manage/event_detail_view_model.dart';
import '../../widgets/title_value_widget.dart';

class EventDetailPage extends BaseStatefulMvvmWidget {
  EventManageListData? item;

  EventDetailPage({this.item});

  @override
  State<StatefulWidget> createState() => _EventDetailState();
}

class _EventDetailState
    extends BaseMvvmState<EventDetailViewModel, EventDetailPage> {
  @override
  Widget buildLoadingContentView() {
    var item = viewModel.item;
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(
            height: 16,
          ),
          Column(
            children: [
              Row(
                children: [
                  CommonText('提报登记', textColor: Colors.black),
                  SizedBox().setWeight(1),
                  CommonText('${item?.eventTypeName??""}', textColor: Colors.blue),
                ],
              ).addToContainer(),
              SizedBox(
                height: 12,
              ),
              if(!(widget.item?.elderName??"").isNullOrEmpty())
                ...[
                  TitleValueWidget(
                    "老人姓名：",
                    nameColor: Colors.grey[700]!,
                    padding: EdgeInsets.symmetric(horizontal: 0),
                    showArrow: false,
                    text: "${widget.item?.elderName??""}",
                    titleWeight: 2,
                    contentWeight: 5,
                  ),
                  SizedBox(
                    height: 12,
                  ),
                  TitleValueWidget(
                    "房间号：",
                    nameColor: Colors.grey[700]!,
                    padding: EdgeInsets.symmetric(horizontal: 0),
                    showArrow: false,
                    text: "${widget.item?.buildingNo??""}${item?.bedNo??""}",
                    titleWeight: 2,
                    contentWeight: 5,
                  ),
                  SizedBox(
                    height: 12,
                  ),
                  TitleValueWidget(
                    "老人编号：",
                    nameColor: Colors.grey[700]!,
                    padding: EdgeInsets.symmetric(horizontal: 0),
                    showArrow: false,
                    text: "${widget.item?.oldManNumber??""}",
                    hint: '',
                    titleWeight: 2,
                    contentWeight: 5,
                  ),
                  SizedBox(
                    height: 12,
                  ),
                ],
              // TitleValueWidget(
              //   "老人编号：",
              //   nameColor: Colors.grey[700],
              //   padding: EdgeInsets.symmetric(horizontal: 0),
              //   showArrow: false,
              //   text: "${item.oldManNumber}",
              //   titleWeight: 1,
              // ),
              // SizedBox(
              //   height: 12,
              // ),
              TitleValueWidget(
                "发生时间：",
                nameColor: Colors.grey[700]!,
                padding: EdgeInsets.symmetric(horizontal: 0),
                showArrow: false,
                text: "${item?.occurrenceTime??""}",
                titleWeight: 1,
              ),
              SizedBox(
                height: 12,
              ),
              TitleValueWidget(
                "发生位置：",
                nameColor: Colors.grey[700]!,
                padding: EdgeInsets.symmetric(horizontal: 0),
                showArrow: false,
                text: "${item?.occurrencePlaceName??""}",
                titleWeight: 1,
              ),
              SizedBox(
                height: 12,
              ),
              TitleValueWidget(
                "详细位置：",
                nameColor: Colors.grey[700]!,
                padding: EdgeInsets.symmetric(horizontal: 0),
                showArrow: false,
                text: "${item?.placeDetail}",
                titleWeight: 1,
              ),
              SizedBox(
                height: 12,
              ),
              TitleValueWidget(
                "事件经过：",
                nameColor: Colors.grey[700]!,
                padding: EdgeInsets.symmetric(horizontal: 0),
                showArrow: false,
                text: "${item?.eventProcess}",
                titleWeight: 1,
              ),
              SizedBox(
                height: 12,
              ),
              TitleValueWidget(
                "登记人：",
                nameColor: Colors.grey[700]!,
                padding: EdgeInsets.symmetric(horizontal: 0),
                showArrow: false,
                text: "${item?.createdByName}",
                titleWeight: 1,
              ),
              SizedBox(
                height: 12,
              ),
              TitleValueWidget(
                "登记时间：",
                nameColor: Colors.grey[700]!,
                padding: EdgeInsets.symmetric(horizontal: 0),
                showArrow: false,
                text: "${item?.createdDate}",
                titleWeight: 1,
              ),
              if ((item?.commonAttaches??[]).isNotEmpty)
                imageGrid(item!.commonAttaches!.map((e) => "${HttpApi.imageHead}${e.id}").toList())
            ],
          ).addToContainer(
              padding: EdgeInsets.symmetric(horizontal: 10, vertical: 16),
              margin: EdgeInsets.symmetric(horizontal: 16),
              decoration: BoxDecoration(color: Colors.white)),
          if ((item?.handleList??[]).isNotEmpty) ...[
            SizedBox(
              height: 16,
            ),
            Column(
              children: [
                Row(
                  children: [
                    CommonText('事件处理', textColor: Colors.black),
                    SizedBox().setWeight(1),
                  ],
                ),
                ...(item?.handleList??[]).map((handle) {
                  return Column(
                    children: [
                      SizedBox(
                        height: 12,
                      ),
                      TitleValueWidget(
                        "处理记录：",
                        nameColor: Colors.grey[700]!,
                        padding: EdgeInsets.symmetric(horizontal: 0),
                        showArrow: false,
                        text: "${handle.detail}",
                        titleWeight: 1,
                      ),
                      SizedBox(
                        height: 12,
                      ),
                      TitleValueWidget(
                        "登记人：",
                        nameColor: Colors.grey[700]!,
                        padding: EdgeInsets.symmetric(horizontal: 0),
                        showArrow: false,
                        text: "${handle.createdBy}",
                        titleWeight: 1,
                      ),
                      SizedBox(
                        height: 12,
                      ),
                      TitleValueWidget(
                        "登记事件：",
                        nameColor: Colors.grey[700]!,
                        padding: EdgeInsets.symmetric(horizontal: 0),
                        showArrow: false,
                        text: "${handle.createdDate}",
                        titleWeight: 1,
                      ),
                      if ((handle.commonAttaches??[]).isNotEmpty)
                        imageGrid((handle.commonAttaches??[]).map((e) => "${HttpApi.imageHead}${e.id}").toList()),

                      SizedBox(
                        height: 12,
                      ),
                    ],
                  ).addToContainer(
                      decoration: BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                                  color: handle == item?.handleList?.last
                                      ? Colors.white
                                      : Colors.grey[350]!,
                                  width: handle == item?.handleList?.last
                                      ? 0
                                      : 1))));
                }).toList(),
              ],
            ).addToContainer(
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 16),
                margin: EdgeInsets.symmetric(horizontal: 16),
                decoration: BoxDecoration(color: Colors.white)),
          ],
          if ((item?.followList??[]).isNotEmpty) ...[
            SizedBox(
              height: 16,
            ),
            Column(
              children: [
                Row(
                  children: [
                    CommonText('事件跟踪', textColor: Colors.black),
                    SizedBox().setWeight(1),
                  ],
                ).addToContainer(),
                ...(item?.followList??[]).map((follow) {
                  return Column(
                    children: [
                      SizedBox(
                        height: 12,
                      ),
                      TitleValueWidget(
                        "跟踪记录：",
                        nameColor: Colors.grey[700]!,
                        padding: EdgeInsets.symmetric(horizontal: 0),
                        showArrow: false,
                        text: "${follow.detail}",
                        titleWeight: 1,
                      ),
                      SizedBox(
                        height: 12,
                      ),
                      TitleValueWidget(
                        "登记人：",
                        nameColor: Colors.grey[700]!,
                        padding: EdgeInsets.symmetric(horizontal: 0),
                        showArrow: false,
                        text: "${follow.createdBy}",
                        titleWeight: 1,
                      ),
                      SizedBox(
                        height: 12,
                      ),
                      TitleValueWidget(
                        "登记时间：",
                        nameColor: Colors.grey[700]!,
                        padding: EdgeInsets.symmetric(horizontal: 0),
                        showArrow: false,
                        text: "${follow.createdDate}",
                        titleWeight: 1,
                      ),
                      if ((follow.commonAttaches??[]).isNotEmpty)
                        imageGrid((follow.commonAttaches??[]).map((e) => "${HttpApi.imageHead}${e.id}").toList()),
                      SizedBox(
                        height: 12,
                      ),
                    ],
                  ).addToContainer(
                      decoration: BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                                  color: follow == item?.followList?.last
                                      ? Colors.transparent
                                      : Colors.grey[350]!,
                                  width: follow == item?.followList?.last
                                      ? 0
                                      : 1))));
                }).toList(),
              ],
            ).addToContainer(
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 16),
                margin: EdgeInsets.symmetric(horizontal: 16),
                decoration: BoxDecoration(color: Colors.white)),
          ]
        ],
      ),
    );
  }

  Widget imageGrid(List<String> files) {
    return GridView.count(
      crossAxisCount: 3,
      crossAxisSpacing: 10,
      mainAxisSpacing: 10,
      children: files.map((e) {
        return Hero(tag: e, child: ImageLoad(e,fit: BoxFit.cover,)).onTap(() {
          showBigImage(context, files, position: files.indexOf(e));
        });
      }).toList(),
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
    ).addToContainer(margin: EdgeInsets.only(top: 16));
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text('事件详情'),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  EventDetailViewModel createViewModel() {
    return EventDetailViewModel(context, item: widget.item);
  }

  @override
  void onRetryClick() {
    // TODO: implement onRetryClick
  }
}
